﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using maximal_clique;

namespace social_network_tool
{
    class CoreFunction
    {
        public static int Compare_CountListInt_asc(List<int> a, List<int> b)
        {
            int lengthA = a.Count;
            int lengthB = b.Count;
            return lengthA.CompareTo(lengthB);
        }

        public static List<float> ChuanHoa(List<float> vector)
        {          
            float max = 0f;
            foreach (float value in vector)
                if (value > max)
                    max = value;
            List<float> result = new List<float>();
            for (int i = 0; i < vector.Count; i++)
                result.Add((float)Math.Round(vector[i] / max, 2));
            return result;
        }

        public static float DoDaiDuongDi(float[,] matrix, int level, List<int> listDsDinh)
        {
            ///int dinhBatDau, dinhKetThuc;
            float length = 0f;
            for (int i = 0; i < listDsDinh.Count - 1; i++)
                length += matrix[listDsDinh[i], listDsDinh[i + 1]];
            return length;
        }

        public static bool IsLetter(string txt)
        {
            char[] arr = txt.ToCharArray();
            foreach (char c in arr)
                if (!Char.IsDigit(c))
                    return false;

            if (txt != "" && int.Parse(txt) < 1)
                return false;

            return true;
        }


        public static void WriteFile_DanhsachKe(List<List<int>> danhsachKe)
        {
            string applicationDirectory = (
  from assembly in AppDomain.CurrentDomain.GetAssemblies()
  where assembly.CodeBase.EndsWith(".exe")
  select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", ""))
  ).FirstOrDefault();
            FileStream fs = new FileStream(applicationDirectory + @"\Testing_DanhSachKe.txt", FileMode.Create);
            StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
            string text;
            for (int i = 0; i < danhsachKe.Count; i++)
            {                
                text = (i + 1) + "::";
                foreach (int dinhke in danhsachKe[i])
                    text += (dinhke + 1)+ "  ";
                writer.WriteLine(text);
            }

            writer.Flush();
            writer.Close();
            fs.Close();                
        }

        public static List<List<int>> LoadFile_DanhsachKe(string path)
        {
            List<List<int>> result = new List<List<int>>();
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader reader = new StreamReader(fs, Encoding.UTF8);
            string text = "";
            while (reader.EndOfStream != true)
            {
                text = reader.ReadLine();
                if (text != "")
                {
                    string[] part = text.Split(new string[] { "::", "  " },StringSplitOptions.RemoveEmptyEntries);
                    result.Add(new List<int>());
                    for (int i = 1; i < part.Length; i++)
                        result[result.Count - 1].Add(int.Parse(part[i]) - 1);
                }
            }
            return result;
        }

        public static int Max(float[] array)
        {
            int idMax = 0;
            for (int i = 1; i < array.Length; i++)
                if (array[i] > array[idMax])
                    idMax = i;
            return idMax;
        }


        public static int Max(List<float> array)
        {
            int idMax = 0;
            for (int i = 1; i < array.Count; i++)
                if (array[i] > array[idMax])
                    idMax = i;
            return idMax;
        }
        #region Compare
        public static int Compare_Item_AscValue1(Item a, Item b)
        {
            return a.value1.CompareTo(b.value1);
        }

        public static int Compare_Item_DesValue1(Item a, Item b)
        {
            return -a.value1.CompareTo(b.value1);
        }

        public static int Compare_Item_AscValue2(Item a, Item b)
        {
            return a.value2.CompareTo(b.value2);
        }

        public static int Compare_Item_DesValue2(Item a, Item b)
        {
            return -a.value2.CompareTo(b.value2);
        }

        public static int Compare_Item_AscValue12(Item a, Item b)
        {
            return (a.value2 + a.value1).CompareTo(b.value1 + b.value2);
        }

        public static int Compare_Item_DesValue12(Item a, Item b)
        {
            return -(a.value2 + a.value1).CompareTo(b.value1 + b.value2);
        }
        #endregion

       
    }
}
